package iot.ht.controller.htuser.service;

import iot.core.data.*;
import iot.core.data.entity.*;
import iot.core.data.mapper.*;
import iot.core.exception.MyException;
import iot.core.manager.auth.AuthManager;
import iot.core.springboot.MyThreadLocal;
import iot.core.util.StreamUtil;
import iot.ht.controller.htuser.data.HTUserDetailQto;
import iot.ht.controller.htuser.data.HTUserDto;
import iot.ht.controller.htuser.data.HTUserQto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.stream.Collectors;

@Slf4j
@Service
public class HTUserService extends ServiceImpl<HTUserMapper, HTUser> implements IHTUserService {
    @Autowired
    HTDBMapper htdbMapper;
    @Autowired
    HTDeptMapper htDeptMapper;
    @Autowired
    HTRoleMapper htRoleMapper;
    @Autowired
    HTUserDeptMapper htUserDeptMapper;
    @Autowired
    HTUserRoleMapper htUserRoleMapper;
    @Autowired
    HTUserServiceMapper htUserServiceMapper;
    @Override
    public HTUserDto InsertInit(BaseQto qm){
        var mpdept = htDeptMapper.getById(qm);
        var mm=new HTUserDto();
        mm.setId(htdbMapper.nextLong() );
        mm.setCrt_DId(mpdept.getId() );
        mm.setCrt_DName(mpdept.getName() );

        return mm;
    }

    @Override
    public HTUserDto Detail(HTUserDetailQto qm){
        var mm=htUserServiceMapper.detail(qm.getId(),qm.getHTDept_Id()  );
        var rl=htUserRoleMapper.getRole(qm.getId(),qm.getHTDept_Id() );
        mm.setRoleList(rl);
        return mm;


    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int Insert(HTUserDto mm){
        var qwold=getBaseMapper().getQueryWrapper();
        qwold.eq("EName",mm.getEName());
        var mold=getBaseMapper().selectOne(qwold);
        if(!ObjectUtils.isEmpty(mold)){
            throw new MyException("账号重复");
        }
        var m = new  HTUser();
        getBaseMapper().dtoToEntity(mm,m);
        m.setPWD(AuthManager.getInstance().createPwd(mm.getEName(),mm.getPWD() ));

        var mdept=new HTUserDept();
        mdept.setId(htdbMapper.nextLong() );
        mdept.setHTUser_Id(m.getId() );
        mdept.setHTDept_Id(mm.getCrt_DId() );
        htUserDeptMapper.insert(mdept,true);

        for(var mr:mm.getRoleList()){
            var mrm=new HTUserRole();
            mrm.setId(htdbMapper.nextLong() );
            mrm.setHTUser_Id(m.getId() );
            mrm.setHTDept_Id(mm.getCrt_DId() );
            mrm.setHTRole_Id(mr.getId());
            htUserRoleMapper.insert(mrm,true);
        }
        return getBaseMapper().insert(m,true);

    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public   int Update(HTUserDto mm){
        var m=getBaseMapper().selectById(mm.getId());
        if(!m.getEName().equals(mm.getEName())){
            var qwold=getBaseMapper().getQueryWrapper();
            qwold.eq("EName",mm.getEName());
            var mold=getBaseMapper().selectOne(qwold);
            if(ObjectUtils.isEmpty(mold)){
                throw new MyException("账号重复");
            }
        }
        getBaseMapper().dtoToEntity(mm,m);
        if(mm.isEditPWD()){
            m.setPWD(AuthManager.getInstance().createPwd(mm.getEName(),mm.getPWD() ));
        }

        var qwur=htUserRoleMapper.getQueryWrapper();
        qwur.eq("HTUser_Id",m.getId() );
        qwur.eq("HTDept_Id",mm.getCrt_DId() );
        qwur.orderBy(true,true,"HTRole_Id");
        var loldr=htUserRoleMapper.selectList(qwur);
        for(var masrc:mm.getRoleList()){
            var mrm = StreamUtil.get(loldr,a->a.getHTRole_Id().equals(masrc.getId()));

            if (mrm == null)
            {
                mrm=new HTUserRole();
                mrm.setId(htdbMapper.nextLong() );
                mrm.setHTUser_Id(m.getId() );
                mrm.setHTDept_Id(mm.getCrt_DId() );
                mrm.setHTRole_Id(masrc.getId());
                htUserRoleMapper.insert(mrm,true);
            }

        }

        for(var madb : loldr)
        {
            var masrc= StreamUtil.get(  mm.getRoleList(), a->a.getId().equals( madb.getHTRole_Id()) );
            if (masrc == null)
            {
                htUserRoleMapper.deleteById(madb,true);
            }
        }

        return getBaseMapper().updateById(m);
    }


    @Override

    public PageData<HTUserDto> Page(HTUserQto qm){
        var r=htUserServiceMapper.page(qm);
        var l=r.getData().stream().map(a->a.getId()).distinct().collect(Collectors.toList());
        var lr=htUserRoleMapper.getRoleByHTDept_IdAndIds(l,qm.getHTDept_Id() );
        for(var mm :r.getData()){
            var mr=StreamUtil.get(lr,a->a.getBId().equals(mm.getId()));
            if(!ObjectUtils.isEmpty(mr)){
                var min=new IdNameDto();
                min.setId(mr.getId());
                min.setName(mr.getName());
                mm.getRoleList().add(min);
            }
        }
        return r;
    }

    @Override
    public int QY(BaseQto mm) {
        var su= MyThreadLocal.get().getSu();
        var qw=htUserDeptMapper.getQueryWrapper();
        qw.eq("HTUser_Id",mm.getId());
        qw.eq("HTDept_Id",su.getCrt_DId() );
        var m=htUserDeptMapper.selectOne(qw);
        var muser=getById(mm.getId() );
        m.setState("启用");
        MyThreadLocal.get().setLogMemo("启用用户:"+muser.getName() );
        return htUserDeptMapper.updateById(m,true);
    }

    @Override
    public int TY(BaseQto mm) {
        var su= MyThreadLocal.get().getSu();
        var qw=htUserDeptMapper.getQueryWrapper();
        qw.eq("HTUser_Id",mm.getId());
        qw.eq("HTDept_Id",su.getCrt_DId() );
        var m=htUserDeptMapper.selectOne(qw);
        var muser=getById(mm.getId() );
        m.setState("停用");
        MyThreadLocal.get().setLogMemo("停用用户:"+muser.getName() );
        return  htUserDeptMapper.updateById(m,true);
    }


}
